<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: awfigq(67877605@qq.com)
// +----------------------------------------------------------------------

// 采集规则类型
class CollectRuleTypeAction extends CommonAction
{
	public function add()
	{
		$new_sort = D("CollectRuleType")->where("status=1") -> max("sort") + 1;
		$this->assign("new_sort",$new_sort);
		
		$this->display();
	}
	
	public function insert()
	{
		$model = D(MODULE_NAME);
		if (false === $model->create())
		{
			$this->error($model->getError());
		}
		//保存当前数据对象
		$id = $model->add();
		if ($id !== false)
		{
			$data = array();
			$data['collect_code'] = trim($_POST['collect_code']);
			$data['web_type'] = intval($_POST['web_type']);
			
			foreach($_POST['api'] as $key => $val)
			{
				$data[$key] = $val;
			}
			
			foreach($data as $key => $value)
			{
				if(!empty($value))
				{
					$rules = array();
					$rules['id'] = $id;
					$rules['name'] = $key;
					$rules['rule'] = $value;
					D("CollectRules")->add($rules);
				}
			}
			
			$this->saveLog(1, $id);
			$this->success(L('ADD_SUCCESS'));
		}
		else
		{
			//失败提示
			$this->saveLog(0, $id);
			$this->error(L('ADD_FAILED'));
		}
	}
	
	public function edit()
	{
		$model = M(MODULE_NAME);
		
		$id = $_REQUEST[$model->getPk()];
		$vo = $model->getById($id);
		$this->assign('vo', $vo);
		
		$rules = D("CollectRules")->where("id=$id")->getField("name,rule");
		
		foreach($rules as $key => $value)
		{
			$rules[$key] = stripslashes($value);
		}
		$this->assign('siteRules', $rules);
		
		$this->display();
	}
	
	public function update()
	{
		$model = D(MODULE_NAME);
		
		$id = $_REQUEST[$model->getPk()];
		
		if (false === $model->create())
		{
			$this->error($model->getError());
		}
		
		// 更新数据
		$bln = $model->save();
		if (false !== $bln)
		{
			D("CollectRules")->where("id=$id")->delete();
			
			$data = array();
			$data['collect_code'] = trim($_POST['collect_code']);
			$data['web_type'] = intval($_POST['web_type']);
			
			foreach($_POST['api'] as $key => $val)
			{
				$data[$key] = $val;
			}

			foreach($data as $key => $value)
			{
				if(!empty($value))
				{
					$site_rules = array();
					$site_rules['id'] = $id;
					$site_rules['name'] = $key;
					$site_rules['rule'] = $value;
					D("CollectRules")->add($site_rules);
				}
			}
			
			//成功提示
			$this->saveLog(1);
			$this->success(L('EDIT_SUCCESS'));
		}
		else
		{
			//错误提示
			$this->saveLog(0);
			$this->error(L('EDIT_FAILED'));
		}
	}
	
	public function foreverdelete ()
	{
		//删除指定记录
		$model = D(MODULE_NAME);
		if (!empty($model))
		{
			$pk = $model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id))
			{
				$condition = array($pk => array('in' , explode(',', $id)));
				if (false !== $model->where($condition)->delete())
				{
					D("SiteRules")->where($condition)->delete();
					$this->saveLog(1);
					$this->success(L('DEL_SUCCESS'));
				}
				else
				{
					$this->saveLog(0);
					$this->error(L('DEL_FAILED'));
				}
			}
			else
			{
				$this->saveLog(0);
				$this->error(L('INVALID_OP'));
			}
		}
		$this->forward();
	}
	
	public function getRules()
	{
		$id = intval($_REQUEST['id']);
		$rules = D("CollectRules")->where("id=$id")->getField("name,rule");
		
		foreach($rules as $key => $value)
		{
			$rules[$key] = stripslashes($value);
		}
		
		echo json_encode($rules);
	}
	
	public function checkKey()
	{
		$key = $_REQUEST['key'];
		
		if(D(MODULE_NAME)->checkKey($key))
			echo 1;
		else
			echo 0;
	}
}
?>